package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.annotation.Log;
import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.form.*;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.IOperatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 运营商管理
 *
 * @author syw
 * @since 2023-09-19
 **/
@RestController
@RequestMapping(value = "/operator")
@Api(tags = "运营商管理")
@RequiredArgsConstructor
public class OperatorController extends BaseController {

    private final IOperatorService operatorService;

    @Log(title = "添加运营商")
    @ApiOperation(value = "管理端-添加运营商")
    @PostMapping("/add")
    public Response<Boolean> add(@RequestBody @Validated AddOperatorForm form) {
        return ResponseFactory.okResponse(operatorService.add(form));
    }

    @Log(title = "删除运营商")
    @ApiOperation(value = "管理端删除运营商")
    @PostMapping("/delete")
    public Response<Boolean> delete(@RequestBody @NotEmpty List<Long> id) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.delete(accountId, id));
    }

    @ApiOperation(value = "管理端-运营商列表")
    @PostMapping("/list")
    public Response<PageVO<OperatorVO>> list(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                             @RequestBody SearchAccountForm form) {
        return ResponseFactory.okResponse(operatorService.list(form, pageable));
    }

    @ApiOperation(value = "管理端-运营商邀请用户列表")
    @PostMapping("/list/invite")
    public Response<PageVO<OperatorInviteVO>> inviteList(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                         @RequestBody @Validated GetOperatorForm form) {
        return ResponseFactory.okResponse(operatorService.inviteList(form, pageable));
    }

    @ApiOperation(value = "运营商统计数据")
    @GetMapping("/get/statistics")
    public Response<List<OperatorStatisticsVO>> getStatistics() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.getStatistics(accountId));
    }

    @ApiOperation(value = "邀请数据统计")
    @GetMapping("/get/invite-statistics")
    public Response<OperatorInviteStatisticsVO> statistics() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.getInviteStatistics(accountId));
    }

    @ApiOperation(value = "积分订单明细")
    @PostMapping("/get/inviteOrderDetail")
    public Response<PageVO<InviteOrderDetailVO>> inviteOrderDetail(@RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.getInviteOrderDetail(accountId, pageNumber, pageSize));
    }

    @ApiOperation(value = "下载运营商统计数据")
    @GetMapping("/download/statistics")
    public Response<Boolean> downloadStatistics(HttpServletResponse response) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.downloadStatistics(response, accountId));
    }

    @ApiOperation(value = "提现")
    @PostMapping("/extract-point")
    public Response<Boolean> extractPoint(@RequestBody @Validated ExtractPointForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.extractPoint(form, accountId));
    }

    @ApiOperation(value = "提现记录")
    @PostMapping("/extract-point-list")
    public Response<PageVO<ExtractPointRecordVO>> extractPointList(Pageable pageable) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(operatorService.getExtractPointRecord(accountId, pageable));
    }


    @ApiOperation(value = "管理端-分享记录查询")
    @PostMapping("/list/query")
    public Response<PageVO<DistributeInfoVO>> distributeInfoList(@RequestBody DistributeQueryVO query, Pageable pageable) {
        return ResponseFactory.okResponse(operatorService.distributeInfoList(query, pageable));
    }


    @ApiOperation(value = "管理端-分享码使用明细")
    @PostMapping("/distribute/detail")
    public Response<List<DistributeCodeUseDetailVO>> distributeDetailInfos(@RequestParam Long distributeId) {
        return ResponseFactory.okResponse(operatorService.distributeDetailInfos(distributeId));
    }


    @ApiOperation(value = "管理端-导出分享码使用明细")
    @PostMapping("/distribute/detail/export")
    public void distributeDetailInfosExport(HttpServletResponse response, @RequestParam Long distributeId) {
        operatorService.distributeDetailInfosExport(response, distributeId);
    }

    @ApiOperation(value = "管理端-导出前100使用次数信息")
    @PostMapping("/distribute/export")
    public void distributeInfosExport(HttpServletResponse response) {
        operatorService.distributeInfosExport(response);
    }


    @ApiOperation(value = "更新人员历史积分")
    @PostMapping("/update/user/history/score")
    public void updateUserHistoryScore() {
        operatorService.updateUserHistoryScore();
    }


}
